package com.example.androiduidemo2;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.androiduidemo2.database.CourseMessage;
import com.example.androiduidemo2.entity.Course;

public class AddCourseActivity extends AppCompatActivity {
    private CourseMessage courseMessage;
    private EditText course_name, course_id, course_teacher, course_class, course_time;
    private Button addCourse;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_course);
        course_name = findViewById(R.id.courseName);
        course_id = findViewById(R.id.courseId);
        course_teacher = findViewById(R.id.courseTeacher);
        course_class = findViewById(R.id.courseClass);
        course_time = findViewById(R.id.courseTime);
        addCourse = findViewById(R.id.addCourse);
        courseMessage = new CourseMessage(this);

        addCourse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addCourse(v); // 当按钮被点击时，调用addCourse方法
            }
        });
    }


    public void addCourse(View view) {
        String courseName = course_name.getText().toString().trim();
        String courseId = course_id.getText().toString().trim();
        String courseTeacher = course_teacher.getText().toString().trim();
        String courseClass = course_class.getText().toString().trim();
        //添加校验
        int courseTime = 0;
        try {
            courseTime = Integer.parseInt(course_time.getText().toString().trim());
        } catch (NumberFormatException e) {
            // 处理解析失败的情况，比如显示错误提示给用户
            Toast.makeText(this, "上课时间必须是有效的整数", Toast.LENGTH_SHORT).show();
            return; // 或者采取其他合适的错误处理方式，比如结束该方法执行
        }

// 非空校验
        if (TextUtils.isEmpty(courseName)) {
            Toast.makeText(this, "课程名称不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(courseId)) {
            Toast.makeText(this, "课程ID不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(courseTeacher)) {
            Toast.makeText(this, "授课教师不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(courseClass)) {
            Toast.makeText(this, "班级信息不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        // 创建课程对象
        Course course = new Course(courseName, courseId, courseTeacher, courseClass, courseTime);
        Log.d("AddCourse", "Adding course with name: " + courseName);

        // 调用数据库操作
        long add = courseMessage.addCourse(course);

        // 再次添加日志输出以查看结果
        Log.d("AddCourse", "Result of adding course: " + add);

        if (add != -1) {
            Toast.makeText(this, "课程添加成功", Toast.LENGTH_SHORT).show();
            goToSettingActivity(); // 跳转到Setting Activity
        } else {
            Toast.makeText(this, "课程添加失败", Toast.LENGTH_SHORT).show();
        }
    }

    private void goToSettingActivity() {
        Intent intent = new Intent(AddCourseActivity.this, SettingActivity.class); // 假设SettingActivity是您要跳转的活动
        startActivity(intent);
        finish(); // 关闭当前AddCourse活动
    }
}
